import axios from 'axios';
import { useNavigate } from 'react-router-dom';
const BASE_URL = process.env.PUBLIC_URL;
const HkzfRequest = axios.create({
  baseURL: BASE_URL,
});
HkzfRequest.interceptors.request.use(
  function (config) {
    let token = window.localStorage.token;
    if (token) {
      config.headers.Authorization = `token ${token}`;
    }
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

HkzfRequest.interceptors.response.use(
  (config) => {
    const { body } = config.data;
    return body;
  },
  (error) => {
    const router = useNavigate();
    //路由跳转
    router('/');
    return Promise.reject(error);
  }
);

export default HkzfRequest;
